package com.sys.product.controller;

import com.sys.common.utils.R;
import com.sys.product.entity.CategoryEntity;
import com.sys.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 商品三级分类
 *
 * @author yangjs
 * @email catalpa65@163.com
 * @date 2020-11-09 21:03:00
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /*
     * 查询树形分类：http://localhost:88/api/product/category/list/tree
     * */
    @RequestMapping("/list/tree")
    public R list() {
        List<CategoryEntity> categoryEntities = categoryService.listWithTree();
        return R.ok().put("data", categoryEntities);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);
        return R.ok();
    }

    /*
     * 删除：
     * @RequestBody 获取POST请求请求体，自动解析JSON: [1,2,3]
     * */
    @RequestMapping("/delete")
    public R delete(@RequestBody List<Long> catIds) {
        categoryService.removeMenunByIds(catIds);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateCascate(category);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

}
